package it.clem.little.domain.combat;

import it.clem.little.domain.ArmorType;
import it.clem.little.domain.AttackType;
import it.clem.little.domain.units.IUnit;
import it.clem.little.domain.units.UnitFactory;

import java.util.Map;
import java.util.TreeMap;

//TODO:capire l'hero multiplier
public enum AttackMultiplier {
  WeakAttack, NormalAttack, StrongAttack, HeroAttack, HeroDefence, HeroVsHero;

  private final static Map<AttackType, Map<ArmorType, AttackMultiplier>> ATTACK_MULTIPLIER_MAP = new TreeMap<AttackType, Map<ArmorType, AttackMultiplier>>();
  static {
    Map<ArmorType, AttackMultiplier> normalAttackMap = new TreeMap<ArmorType, AttackMultiplier>();
    normalAttackMap.put(ArmorType.Light, StrongAttack);
    normalAttackMap.put(ArmorType.Heavy, WeakAttack);
    normalAttackMap.put(ArmorType.Cloth, NormalAttack);
    normalAttackMap.put(ArmorType.Hero, HeroDefence);
    ATTACK_MULTIPLIER_MAP.put(AttackType.Normal, normalAttackMap);

    Map<ArmorType, AttackMultiplier> magicAttackMap = new TreeMap<ArmorType, AttackMultiplier>();
    magicAttackMap.put(ArmorType.Light, NormalAttack);
    magicAttackMap.put(ArmorType.Heavy, StrongAttack);
    magicAttackMap.put(ArmorType.Cloth, WeakAttack);
    magicAttackMap.put(ArmorType.Hero, HeroDefence);
    ATTACK_MULTIPLIER_MAP.put(AttackType.Magic, magicAttackMap);

    Map<ArmorType, AttackMultiplier> piercingAttackMap = new TreeMap<ArmorType, AttackMultiplier>();
    piercingAttackMap.put(ArmorType.Light, WeakAttack);
    piercingAttackMap.put(ArmorType.Heavy, NormalAttack);
    piercingAttackMap.put(ArmorType.Cloth, StrongAttack);
    piercingAttackMap.put(ArmorType.Hero, HeroDefence);
    ATTACK_MULTIPLIER_MAP.put(AttackType.Piercing, piercingAttackMap);

    Map<ArmorType, AttackMultiplier> heroAttackMap = new TreeMap<ArmorType, AttackMultiplier>();
    heroAttackMap.put(ArmorType.Light, HeroAttack);
    heroAttackMap.put(ArmorType.Heavy, HeroAttack);
    heroAttackMap.put(ArmorType.Cloth, HeroAttack);
    heroAttackMap.put(ArmorType.Hero, HeroVsHero);// TODO:cosa ci va qua?
    ATTACK_MULTIPLIER_MAP.put(AttackType.Hero, heroAttackMap);
  }

  private AttackMultiplier() {
  }

  public static AttackMultiplier getAttackMultiplier(AttackType attack, ArmorType armor) {
    return ATTACK_MULTIPLIER_MAP.get(attack).get(armor);
  }

  public static double getAttackMultiplierValue(AttackType attack, ArmorType armor) {
    double res = 0;
    switch (getAttackMultiplier(attack, armor)) {
    case WeakAttack:
      res=.75;
      break;
    case NormalAttack:
      res=1;
      break;
    case StrongAttack:
      res=2;
      break;
    //case HeroAttack:break;
    //case HeroDefence:break;
    //case HeroVsHero:break;
    default:
      throw new IllegalStateException();
    }
    return res;
  }

  public static int getAttackValue(IUnit attacker,IUnit defender){
    if (attacker.isNonCombatant()) return 0;
    double multiplier = getAttackMultiplierValue(attacker.getAttackType(), defender.getArmorType());
    return (int) Math.floor((attacker.getAttackPoints() - defender.getArmorPoints())*multiplier);
  }
  
  public static void main (String... s){
    System.out.println(getAttackValue(UnitFactory.getIronWheel(), UnitFactory.getKnight()));
    //System.out.println(getAttackValue(UnitFactory.NINJA, UnitFactory.KNIGHT));
  }
}
